package com.augurit.gzcgwsp.wfex.app.doc.impl;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.printDoc.dao.YwPrintDocDao;
import com.augurit.gzcgwsp.wfex.app.doc.IOaDocCodeServiceEX;
import com.augurit.gzcgwsp.wfex.common.dao.OaDocCodeDaoEX;
import com.augurit.wf.app.doccode.service.IOaDocCodeService;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.common.entity.OaDocCode;

@Service
@Transactional
public class OaDocCodeServiceImplEX implements IOaDocCodeServiceEX{

	@Autowired
	private OaDocCodeDaoEX oaDocCodeDaoEX;
	
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	
	@Autowired
	private IOaDocCodeService oaDocCodeService;
	@Autowired
	private YwPrintDocDao ywPrintDocDao;
	
	public IOaDocCodeService getOaDocCodeService(){
		return this.oaDocCodeService;
	}
	
	public Long getCurSeqAndAddSeq(String templateCode, String fileCode,Long ajId) {
		String year=DateUtilsEX.getCurYear();
	    Long curSeq = 0L;
	    String sql=" select count(*) from YW_PRINT_DOC 　where file_code='"+fileCode+"' and  " +
	    		" substr(to_char(creat_date,'yyyy-MM-dd'), 0, 4)='"+year+"'";
	  //每个业务处的不同
        if(("ccgbggcs".equals(templateCode) || "csdllszysp".equals(templateCode)
        		||"csdlwj".equals(templateCode))
        		&& "zyxzxkjds".equals(fileCode)){
        	//每个业务或者每个处室准予许可不能全委一致
        	sql=" select count(*) from YW_PRINT_DOC 　where file_code='"+fileCode+"' " +
        			"and template_code='"+templateCode+"'"+" and  " +
    	    		" substr(to_char(creat_date,'yyyy-MM-dd'), 0, 4)='"+year+"'";
        }else if((templateCode.contains("rqjyxkz") || "rqssgd".equals(templateCode) 
        		|| "rqrsqjazwxqyzz".equals(templateCode)) && !"bzcltzs".equals(fileCode)){
        	//燃气
        	sql=" select count(*) as countnum from YW_PRINT_DOC 　where file_code='"+fileCode+"' " +
        			" and template_code in ('rqjyxkz_pzqy','rqjyxkz_gdqy','rqssgd','rqrsqjazwxqyzz') "+" and  " +
    	    		" substr(to_char(creat_date,'yyyy-MM-dd'), 0, 4)='"+year+"'";
        }else if ("hwggshsqb".equals(templateCode) && "byxkjds".equals(fileCode)){
        	//户外广告
        	sql=" select count(*) from YW_PRINT_DOC 　where file_code='"+fileCode+"' " +
        			"and template_code='"+templateCode+"'"+" and  " +
    	    		" substr(to_char(creat_date,'yyyy-MM-dd'), 0, 4)='"+year+"'";
        }else if("shljys".equals(templateCode) || "shljcz".equals(templateCode)
        		||"shljqssj".equals(templateCode)){
        	//环卫处
        	sql=" select count(*) from YW_PRINT_DOC 　where file_code='"+fileCode+"'" +
        			" and template_code in('shljqssj','shljcz','shljqssj')"+" and  " +
    	    		" substr(to_char(creat_date,'yyyy-MM-dd'), 0, 4)='"+year+"'";;
        }
        
        Query query=ywPrintDocDao.getSession().createSQLQuery(sql);
         List list=query.list();
         int count=((BigDecimal) list.get(0)).intValue();
         curSeq=Long.valueOf(count)+1;
		return curSeq;
	}

   private Long getMaxNum(String word){
        Long maxnum=0l;
        String hql = "select nvl(max(seq),0) from "+OaDocCode.class.getName()+" where word= '" + word +"' and year='"+DateUtilsEX.getCurYear()+"'";
        List<Long> list = oaDocCodeDaoEX.find(hql);
        if(list != null && list.size()>0){
            maxnum = list.get(0);
            if(maxnum!=null){
            	return maxnum;
            }else{
            	maxnum=0l;
            }
        }
        return maxnum;
     }
	    
	
    public Long getMaxCurSeqAndAddSeq(String templateCode, String word) {
        Long curSeq = 0L;
//        List<OaDocCode> list = oaDocCodeDaoEX.get(templateCode, word);
//        if(null != list && list.size() > 0){
//            OaDocCode temp = list.get(0);
//            curSeq = temp.getSeq();
//        }
        curSeq=this.getMaxNum(word);
        return curSeq;
    }

    public void saveSeq(String templateCode,String fileCode, Long ywId,String isunified) {
        Long curSeq = 0L;
        List<OaDocCode> list = oaDocCodeDaoEX.getOaDocCode(templateCode,fileCode, ywId);
        OaDocCode code=null;
        if(list!=null && list.size()>0){
             code=list.get(0);
        }else{
            code=new OaDocCode();
            if("false".equals(isunified)){
            	   curSeq=getMaxNum(fileCode,templateCode)+1;
            }else{
            	   curSeq=getMaxNum(fileCode)+1;
            }
//            List<OaDocCode> list1 = oaDocCodeDaoEX.get(templateCode, fileCode);
//            if(null != list1 && list1.size() > 0){
//                OaDocCode temp = list1.get(0);
//                curSeq = temp.getSeq()+1;
//            }
            code.setSeq(curSeq);
            code.setTemplateCode(templateCode);
            code.setYwId(ywId);
            code.setWord(fileCode);
            code.setYear(DateUtilsEX.getCurYear());
            code.setUnit("号");
            code.setTemplate("template");
            oaDocCodeDaoEX.save(code);
        }
    }

    /*
     * 生成编号
     * @see com.augurit.gzcgwsp.wfex.app.doc.IOaDocCodeServiceEX#getCurSeqAndAddSeq(java.lang.String, java.lang.String, java.lang.Long, boolean)
     */
	public Long getCurSeqAndAddSeq(String templateCode, String fileCode,
			Long ajId, boolean isunified) {
		if(isunified){
			return getCurSeqAndAddSeq(templateCode, fileCode, ajId);
		}else{
			   Long curSeq = 0L;
			    List<OaDocCode> list = oaDocCodeDaoEX.getOaDocCode(templateCode,fileCode, ajId);
			    if(list!=null && list.size()>0){
			        curSeq=list.get(0).getSeq();
			    }else{
			      Long num=this.getMaxNum(fileCode,templateCode);
			      if(num!=null){
			    	   curSeq=num+1;
			      }else{
			    	  curSeq=1L;
			      }
			    }
				return curSeq;
		}
	}
	
	/**
	 * 每个业务的最大编号
	 * @param word
	 * @param templateCode
	 * @return
	 */
	 private Long getMaxNum(String word,String templateCode){
	        Long maxnum=0l;
	        String hql = "select nvl(max(seq),0) from "+OaDocCode.class.getName()+" where word= '" + word 
	        			+"' and year='"+DateUtilsEX.getCurYear()+"' and templateCode='"+templateCode+"'";
	        List<Long> list = oaDocCodeDaoEX.find(hql);
	        if(list != null && list.size()>0){
	            maxnum = list.get(0);
	            if(maxnum!=null){
	            	return maxnum;
	            }else{
	            	maxnum=0l;
	            }
	        }
	        return maxnum;
	     }

	 /**
	  * 燃气处的许可书编号
	  */
	public Long getMaxSeqByGas(String templateCode, String fileCode,
			Long ajId, boolean isunified) {
		if(isunified){
			return getCurSeqAndAddSeq(templateCode, fileCode, ajId);
		}else{
			   Long curSeq = 0L;
			    List<OaDocCode> list = oaDocCodeDaoEX.getOaDocCode(templateCode,fileCode, ajId);
			    if(list!=null && list.size()>0){
			        curSeq=list.get(0).getSeq();
			    }else{
			      Long num=this.getMaxGasNum(fileCode,templateCode);
			      if(num!=null){
			    	   curSeq=num+1;
			      }else{
			    	  curSeq=1L;
			      }
			    }
				return curSeq;
		}
	}
	
	
	/**
	 * 每个业务的最大编号
	 * @param word
	 * @param templateCode
	 * @return
	 */
	 private Long getMaxGasNum(String word,String templateCode){
	        Long maxnum=0l;
	        String hql = "select count(id) from "+OaDocCode.class.getName()+" where word= '" + word 
	        			+"' and year='"+DateUtilsEX.getCurYear()+"' and templateCode in ('rqjyxkz_pzqy','rqjyxkz_gdqy','rqrsqjazwxqyzz','rqssgd')";
	        List<Long> list = oaDocCodeDaoEX.find(hql);
	        if(list != null && list.size()>0){
	            maxnum = list.get(0);
	            if(maxnum!=null){
	            	return maxnum;
	            }else{
	            	maxnum=0l;
	            }
	        }
	        return maxnum;
	     }

	@Override
	public Long getQuCurSeqAndAddSeq(String templateCode, String fileCode,
			Long id,LoginUserForm user) {

		String year=DateUtilsEX.getCurYear();
	    Long curSeq = 0L;
	    String sql=" select count(*) from YW_PRINT_DOC doc,wf_bus_instance ins, wf_bus_template temp where " +
	    		" ins.master_entity_key=doc.yw_id and temp.id=ins.template_id and doc.template_code=temp.template_code " +
	    		"and  file_code='"+fileCode+"' and  bus_memo2='" +user.getOrg().getOrgName().substring(0,3)+"'"+
	    		" and  substr(to_char(creat_date,'yyyy-MM-dd'), 0, 4)='"+year+"'";
	  //每个业务处的不同
        if(("csdllszyspqx".equals(templateCode)||"csdlwjqx".equals(templateCode))&& "quzyxzxkjds".equals(fileCode)){
        	//每个业务或者每个处室准予许可不能全委一致
        	sql=" select count(*) from YW_PRINT_DOC doc,wf_bus_instance ins, wf_bus_template temp where " +
	    		" ins.master_entity_key=doc.yw_id and temp.id=ins.template_id and doc.template_code=temp.template_code " +
	    		"and  file_code='"+fileCode+"' and  bus_memo2='" +user.getOrg().getOrgName().substring(0,3)+"'"+
	    		" and  substr(to_char(creat_date,'yyyy-MM-dd'), 0, 4)='"+year+"' and doc.template_code='"+templateCode+"'";
        }else if((templateCode.equals("rqjyxkz") || "rqssgd".equals(templateCode) 
        		|| "rqrsqjazwxqyzz".equals(templateCode)) && !"bzcltzs".equals(templateCode)){
        	//燃气
        	sql=" select count(*) from YW_PRINT_DOC doc,wf_bus_instance ins, wf_bus_template temp where " +
    		" ins.master_entity_key=doc.yw_id and temp.id=ins.template_id and doc.template_code=temp.template_code " +
    		"and  file_code='"+fileCode+"' and  bus_memo2='" +user.getOrg().getOrgName().substring(0,3)+"'"+
    		" and  substr(to_char(creat_date,'yyyy-MM-dd'), 0, 4)='"+year+"' and doc.template_code in ('rqjyxkzqx_pzqy','rqjyxkzqx_gdqy')";

        }
        
        Query query=ywPrintDocDao.getSession().createSQLQuery(sql);
         List list=query.list();
         int count=((BigDecimal) list.get(0)).intValue();
         curSeq=Long.valueOf(count)+1;
		return curSeq;
	
	}

}
